package com.lobo.blog.handler;

import com.lobo.blog.exception.NotFoundException;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Logger;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/9/25
 * @description:统一异常处理
 */
@ControllerAdvice
public class ControllerExceptionHandler implements HandlerExceptionResolver {

    /**
     * * 声明该方法是异常处理的方法 <br>
     * * 返回值可以是String，也可以是ModelAndView
     *
     * @param httpServletRequest  请求信息
     * @param httpServletResponse 返回信息
     * @param o                   出现异常的对象
     * @param e                   出现的异常
     * @return modelAndView 返回的信息和页面
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ExceptionInfo", e.getMessage());
        modelAndView.addObject("url", httpServletRequest.getRequestURI());
        modelAndView.setViewName("error/error");

        return modelAndView;
    }

}
    
